﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using SharpArch.Domain.DomainModel;

namespace DevelopmentKit.Cms.Domain
{
    /// <summary>
    /// 内容
    /// </summary>
    public class Content : Entity 
    {
        /// <summary>
        /// 标题
        /// </summary>
        [Display(Name = "标题")]
        [Required(ErrorMessage = "{0}不能为空")]
        [StringLength(200, ErrorMessage = "{0}取值长度{1}~{2}个字符", MinimumLength = 0)]
        public virtual string Title { get; set; }
         

        /// <summary>
        /// 内容类型
        /// </summary>
        [Display(Name = "内容类型")]
        [Required(ErrorMessage = "{0}不能为空")] 
        public virtual Definition Definition { get; set; }
      
        /// <summary>
        /// 父级内容
        /// </summary>
        [Display(Name = "父级内容")]
        public virtual Content Parent { get; set; }


        /// <summary>
        /// 归属的站点
        /// </summary>
        [Display(Name = "归属的站点")]
        public virtual Content Root { get; set; }

      
        /// <summary>
        /// 名称,用于Url优化
        /// </summary>
        [Display(Name = "Url")]
        [Required(ErrorMessage = "{0}不能为空")]
        [StringLength(200, ErrorMessage = "{0}取值长度{1}~{2}个字符", MinimumLength = 0)]
        public virtual string Name { get; set; }


        /// <summary>
        /// 内容详情
        /// </summary>
        [Display(Name = "内容详情")] 
        [StringLength(int.MaxValue, ErrorMessage = "{0}取值长度{1}~{2}个字符", MinimumLength = 0)]
        public virtual string Detail { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [Display(Name = "创建时间")]
        [Required(ErrorMessage = "{0}不能为空")]
        public virtual DateTime Created { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [Display(Name = "修改时间")]
        [Required(ErrorMessage = "{0}不能为空")]
        public virtual DateTime Modified { get; set; }

        /// <summary>
        /// 发布时间
        /// </summary>
        [Display(Name = "发布时间")]
        public virtual DateTime? Published { get; set; }

        /// <summary>
        /// 失效时间
        /// </summary>
        [Display(Name = "失效时间")] 
        public virtual DateTime? Expires { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        [Display(Name = "排序号")]
        [Required(ErrorMessage = "{0}不能为空")]
        public virtual int SortOrder { get; set; }

        /// <summary>
        /// 是否显示
        /// </summary>
        [Display(Name = "是否显示")]
        [Required(ErrorMessage = "{0}不能为空")]
        public virtual bool Visible { get; set; }
        

        /// <summary>
        /// 修改人
        /// </summary>
        [Display(Name = "修改人")]
        [Required(ErrorMessage = "{0}不能为空")]
        public virtual int Saver { get; set; }


         
        /// <summary>
        /// 内容状态
        /// </summary>
        [Display(Name = "状态")]
        [Required(ErrorMessage = "{0}不能为空")]
        public virtual ContentState State { get; set; }

        /// <summary>
        /// 内容详情
        /// </summary>
        [Display(Name = "内容详情")]
        public virtual IList<ContentDetail> ContentDetails { get; set; }

        /// <summary>
        /// 内容详情
        /// </summary>
        [Display(Name = "内容详情")]
        public virtual IDictionary<string, ContentDetail> Details { get; protected set; }
        /// <summary>
        /// 子内容
        /// </summary>
        [Display(Name = "子内容")]
        public virtual IList<Content> Children { get; set; } 

       

        /// <summary>
        /// 获取内容详情项
        /// </summary>
        /// <param name="detailName"></param>
        /// <returns></returns>
        public virtual string GetDetail(string detailName)
        {
            if (null == Details)
            {
                Details = ContentDetails.ToDictionary(k => k.Name, v => v);
            } 
            return Details.ContainsKey(detailName)
                ? Details[detailName].Value
                : null;
        }
    }
}
 
